package com.example.scaledrone.chat;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;


public class MessageAdapter extends BaseAdapter {
	final private  static String LOG_TAG="chat";
    public int []layouts={
	R.layout.bg_incoming,
	R.layout.bg_outgoing,		
	R.layout.bg_incoming_speech,
	R.layout.bg_outgoing_speech,			
	R.layout.itemincoming,
	R.layout.itemoutgoing,
	R.layout.itemreceive,
	R.layout.itemsend,
	R.layout.speech_bubble_nice_patch_agent,
	R.layout.speech_bubble_nice_patch_user,
	R.layout.speech_bubble_simple_agent,
	R.layout.speech_bubble_simple_user,
	R.layout.speech_bubble_simple_left,
    R.layout.speech_bubble_simple_right,
	R.layout.singlemessage_a,
	R.layout.singlemessage_b,
	R.layout.singlemessage_up,
	R.layout.singlemessage_down,
	R.layout.singlemessage_right,
	R.layout.singlemessage_left,
	R.layout.my_message,
	R.layout.their_message};
    List<Message> messages = new ArrayList<Message>();
    Context context;

    public MessageAdapter(Context context) {
        this.context = context;
    }


    public void add(Message message) {
        this.messages.add(message);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int i) {
        return messages.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
            try{
				LayoutInflater messageInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

				int layout_id=layouts[i%layouts.length];

				convertView = messageInflater.inflate(layout_id, null);
				MessageViewHolder holder=new MessageViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.messageBody = (TextView) convertView.findViewById(R.id.message);
				Message message = messages.get(i);
				
				Log.d(LOG_TAG,message.getMemberData().getName());
				Log.d(LOG_TAG,message.getText());
				if(holder.name!=null){
				holder.name.setText(message.getMemberData().getName());
				}
				if(holder.messageBody!=null){
				holder.messageBody.setText(message.getText());
				}
				convertView.setTag(holder);
				//GradientDrawable drawable = (GradientDrawable) holder.avatar.getBackground();
				//drawable.setColor(Color.parseColor(message.getMemberData().getColor()));
			} catch(Exception ex)
			{
				Log.d(LOG_TAG, Log.getStackTraceString(ex));
			}
        

        return convertView;
    }

}

class MessageViewHolder {
    public View avatar;
    public TextView name;
    public TextView messageBody;
}